﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using hygge_imaotai.Entity;
using hygge_imaotai.Repository;
using imaotai.Entity;

namespace hygge_imaotai.Domain
{
    /// <summary>
    /// 用户管理 - 搜索的condition
    /// </summary>
    public class ReservationResultViewModel : ViewModelBase
    {
        #region Fields

        // 搜索条件
        private string? _phone;
        private string? _userId;
        private string _status;

        // 分页数据
        private long _total = 0;
        private int _current = 1;
        private int _pageSize = 10;
        private long _pageCount = 0;

        #endregion

        #region Properties

        public string? Phone
        {
            get => _phone;
            set => SetProperty(ref _phone, value);
        }

        public string? UserId
        {
            get => _userId;
            set => SetProperty(ref _userId, value);
        }

        public string? Status
        {
            get => _status;
            set => SetProperty(ref _status, value);
        }


        public static ObservableCollection<ReservationResultEntity> ReservationResultList { get; } =
            new ObservableCollection<ReservationResultEntity>();


        public long Total
        {
            get => _total;
            set => SetProperty(ref _total, value);
        }

        public int Current
        {
            get => _current;
            set => SetProperty(ref _current, value);
        }

        public int PageSize
        {
            get => _pageSize;
            set => SetProperty(ref _pageSize, value);
        }

        public long PageCount
        {
            get => _pageCount;
            set => SetProperty(ref _pageCount, value);
        }

        #endregion

        #region Constructor

        public ReservationResultViewModel()
        {
            CurrentPageChangeCommand = new AnotherCommandImplementation(UpdateData);
        }

        #endregion

        #region DelegateCommand
        public ICommand CurrentPageChangeCommand { get; private set; }
        private void UpdateData(object parameter)
        {
            ReservationResultList.Clear();
            DB.Sqlite.Select<ReservationResultEntity>()
                .WhereIf(!string.IsNullOrEmpty(this.Phone),
                    i => i.MobilePhone.Contains(this.Phone))
                .Page((int)parameter, 10).ToList().ForEach(ReservationResultList.Add);
        }

        #endregion
    }
}
